package com.spic.business.hidden.management.service.impl;

import com.spic.business.common.datascope.anotation.BusinessDataScope;
import com.spic.business.hidden.management.domain.HiddenLibraryManagement;
import com.spic.business.hidden.management.mapper.HiddenLibraryManagementMapper;
import com.spic.business.hidden.management.service.IHiddenLibraryManagementService;
import com.spic.business.hidden.place.domain.HiddenPlace;
import com.spic.business.hidden.place.mapper.HiddenPlaceMapper;
import com.spic.business.hidden.type.domain.HiddenType;
import com.spic.business.hidden.type.mapper.HiddenTypeMapper;
import com.spic.common.core.utils.DateUtils;
import com.spic.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 排查清单库管理Service业务层处理
 *
 * @author spic
 * @date 2021-10-15
 */
@Service
public class HiddenLibraryManagementServiceImpl implements IHiddenLibraryManagementService
{
    @Autowired
    private HiddenLibraryManagementMapper hiddenLibraryManagementMapper;

    @Autowired
    private HiddenTypeMapper hiddenTypeMapper;
    @Autowired
    private HiddenPlaceMapper hiddenPlaceMapper;
    /**
     * 查询排查清单库管理
     *
     * @param id 排查清单库管理主键
     * @return 排查清单库管理
     */
    @Override
    public HiddenLibraryManagement selectHiddenLibraryManagementById(Long id)
    {
        HiddenLibraryManagement rows  =  hiddenLibraryManagementMapper.selectHiddenLibraryManagementById(id);

        return rows;
    }

    /**
     * 查询排查清单库管理列表
     *
     * @param hiddenLibraryManagement 排查清单库管理
     * @return 排查清单库管理
     */
    @Transactional
    @Override
    @BusinessDataScope(menuPath = "business/hidden/management/index")
    public List<HiddenLibraryManagement> selectHiddenLibraryManagementList(HiddenLibraryManagement hiddenLibraryManagement)
    {
        List<HiddenLibraryManagement> hiddenLibraryManagements =  hiddenLibraryManagementMapper.selectHiddenLibraryManagementList(hiddenLibraryManagement);
        if(StringUtils.isNotNull(hiddenLibraryManagements) && hiddenLibraryManagements.size()>0){
            hiddenLibraryManagements.forEach(hiddenLList ->{
            String live = hiddenLList.getHiddenType();
            if(live!=null && !"".equals(live)){
                HiddenType hiddenTypes = hiddenTypeMapper.selectHiddenTypeById(live);
                if(hiddenTypes!=null){
                    hiddenLList.setHiddenType(hiddenTypes.getHiddenTypeName());
                }
            }
            });
            hiddenLibraryManagements.forEach(secondaryHiddenDanger ->{
             String lives = secondaryHiddenDanger.getSecondaryHiddenDanger();
             if(lives!=null && !"".equals(lives)){
                 HiddenType hiddenType = hiddenTypeMapper.selectHiddenTypeById(lives);
                 if(hiddenType!=null){
                     secondaryHiddenDanger.setSecondaryHiddenDanger(hiddenType.getHiddenTypeName());
                 }
             }
            });
            hiddenLibraryManagements.forEach(hiddenPlaceList ->{
            String hiddenPlace = hiddenPlaceList.getHiddenPlace();
            if(hiddenPlace!=null && !"".equals(hiddenPlace)){
                HiddenPlace hiddenTypes = hiddenPlaceMapper.selectHiddenPlaceById(hiddenPlace);
                if(hiddenTypes!=null){
                    hiddenPlaceList.setHiddenPlace(hiddenTypes.getPlaceName());
                }
            }
            });
            hiddenLibraryManagements.forEach(hiddenDangerList ->{
             String hiddenDangers = hiddenDangerList.getHiddenDanger();
             if(hiddenDangers!=null && !"".equals(hiddenDangers)){
                 HiddenPlace hiddenTypeLive =hiddenPlaceMapper.selectHiddenPlaceById(hiddenDangers);
                 if(hiddenTypeLive!=null){
                     hiddenDangerList.setHiddenDanger(hiddenTypeLive.getPlaceName());
                 }
             }
            });
        }
        return (hiddenLibraryManagements);
    }

    /**
     * 新增排查清单库管理
     *
     * @param hiddenLibraryManagement 排查清单库管理
     * @return 结果
     */
    @Override
    public int insertHiddenLibraryManagement(HiddenLibraryManagement hiddenLibraryManagement)
    {
        hiddenLibraryManagement.setCreateTime(DateUtils.getNowDate());
        HiddenType hiddenTypes = hiddenTypeMapper.selectHiddenTypeById(hiddenLibraryManagement.getHiddenType());
        if(hiddenTypes!=null){
            hiddenLibraryManagement.setHiddenType(hiddenTypes.getHiddenTypeName());
        }
        HiddenType hiddenTypeTwo = hiddenTypeMapper.selectHiddenTypeById(hiddenLibraryManagement.getSecondaryHiddenDanger());
        if(hiddenTypeTwo!=null){
            hiddenLibraryManagement.setSecondaryHiddenDanger(hiddenTypes.getHiddenTypeName());
        }
        HiddenPlace hiddenPlace = hiddenPlaceMapper.selectHiddenPlaceById(hiddenLibraryManagement.getHiddenPlace());
        if(hiddenPlace!=null){
            hiddenLibraryManagement.setHiddenPlace(hiddenPlace.getPlaceName());
        }
        HiddenPlace hiddenPlaceTwo = hiddenPlaceMapper.selectHiddenPlaceById(hiddenLibraryManagement.getHiddenDanger());
        if(hiddenPlaceTwo!=null){
            hiddenLibraryManagement.setHiddenDanger(hiddenPlaceTwo.getPlaceName());

        }

        return hiddenLibraryManagementMapper.insertHiddenLibraryManagement(hiddenLibraryManagement);
    }

    /**
     * 修改排查清单库管理
     *
     * @param hiddenLibraryManagement 排查清单库管理
     * @return 结果
     */
    @Override
    public int updateHiddenLibraryManagement(HiddenLibraryManagement hiddenLibraryManagement)
    {
        hiddenLibraryManagement.setUpdateTime(DateUtils.getNowDate());
        HiddenType hiddenTypes = hiddenTypeMapper.selectHiddenTypeById(hiddenLibraryManagement.getHiddenType());
        if(hiddenTypes!=null){
            hiddenLibraryManagement.setHiddenType(hiddenTypes.getHiddenTypeName());
        }
        HiddenType hiddenTypeTwo = hiddenTypeMapper.selectHiddenTypeById(hiddenLibraryManagement.getSecondaryHiddenDanger());
        if(hiddenTypeTwo!=null){
            hiddenLibraryManagement.setSecondaryHiddenDanger(hiddenTypes.getHiddenTypeName());
        }
        HiddenPlace hiddenPlace = hiddenPlaceMapper.selectHiddenPlaceById(hiddenLibraryManagement.getHiddenPlace());
        if(hiddenPlace!=null){
            hiddenLibraryManagement.setHiddenPlace(hiddenPlace.getPlaceName());
        }
        HiddenPlace hiddenPlaceTwo = hiddenPlaceMapper.selectHiddenPlaceById(hiddenLibraryManagement.getHiddenDanger());
        if(hiddenPlaceTwo!=null){
            hiddenLibraryManagement.setHiddenDanger(hiddenPlaceTwo.getPlaceName());

        }
        return hiddenLibraryManagementMapper.updateHiddenLibraryManagement(hiddenLibraryManagement);
    }

    /**
     * 批量删除排查清单库管理
     *
     * @param ids 需要删除的排查清单库管理主键
     * @return 结果
     */
    @Override
    public int deleteHiddenLibraryManagementByIds(Long[] ids)
    {
        return hiddenLibraryManagementMapper.deleteHiddenLibraryManagementByIds(ids);
    }

    /**
     * 删除排查清单库管理信息
     *
     * @param id 排查清单库管理主键
     * @return 结果
     */
    @Override
    public int deleteHiddenLibraryManagementById(Long id)
    {
        return hiddenLibraryManagementMapper.deleteHiddenLibraryManagementById(id);
    }
}
